<?php
namespace App\Admin\Controllers\Products;

use App\Models\Product\ProductClassify;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
class ClassifyFileController extends AdminController{

    protected $title = '品类文件管理';

    protected function grid()
    {
        Admin::style($this->_style());
        $grid = new Grid(new ProductClassify());
        $grid->model()->orderBy('id', 'DESC');
        $grid->column('id','ID');
        $grid->column('season','季节性')->display(function ($v){
            return ProductClassify::$_season[$v];
        })->filter(ProductClassify::$_season);
        $grid->column('title','品类');

        $grid->column('product_ppt.ppt','产品资料')->display(function ($v){
            return ProductClassify::getFiles($v);
        });
        $grid->column('quality.qualities','品检信息')->display(function ($v){
            return ProductClassify::getFiles($v);
        });
        $grid->column('package_material.packages','生产资料')->display(function ($v){
            return ProductClassify::getFiles($v);
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
        });
        $grid->disableCreateButton();
        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->disableFilter();
        $grid->quickSearch('title');

        return $grid;
    }

    protected function _style()
    {
        return <<<STYLE
.table > thead > tr > th, .table > tbody > tr > td {
    white-space: nowrap;
}
.table-responsive {
    max-height: calc(80vh);
}
STYLE;
    }

    protected function form()
    {
        $form = new Form(new ProductClassify());
        $form->display('season','季节性')->with(function ($value){
            return ProductClassify::$_season[$value];
        })->setWidth(5);
        $form->display('title','品类')->setWidth(5);

        $form->multipleFile('product_ppt.ppt','产品资料')->removable()->sortable()->sequenceName()->setWidth(5);
        $form->multipleFile('package_material.packages','生产资料')->removable()->sortable()->sequenceName()->setWidth(5);
        $form->multipleFile('quality.qualities','品检资料')->removable()->sortable()->sequenceName()->setWidth(5);

        $form->tools(function (Form\Tools $tools){
            $tools->disableDelete();
        });

        $form->footer(function (Form\Footer $footer){
            $footer->disableReset();
        });

        return $form;
    }

    protected function detail($id)
    {
        $show = new Show(ProductClassify::with(['product_ppt','package_material','quality'])->findOrFail($id));
        $show->season('季节性')->as(function ($value){
            return ProductClassify::$_season[$value];
        });
        $show->title('品类');
        $show->field('product_ppt.ppt','产品资料')->multipleFile();
        $show->field('package_material.packages','生产资料')->multipleFile();
        $show->field('quality.qualities','品检资料')->multipleFile();
        $show->panel()->tools(function (Show\Tools $tools) {
            $tools->disableDelete();
        });

        return $show;
    }
}
